package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.Recharge;
import com.gxa.p2p.service.RechargeService;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cxy
 * Date: 2020/08/07 16:10
 * Description:
 * Version: V1.0
 */
@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/recharge")
public class RechargeController {
    @Resource(name = "rechargeService")
    RechargeService rechargeService;

    /**
     * 充值记录全查询
     */
    @GetMapping
    public Map<String,Object> selectAll ()
    {   MapUtil mapUtil = new MapUtil();
        List<Recharge> recharges = rechargeService.selectAll();
        System.out.println("list"+recharges);
        if (recharges.size()>0)
        {
            mapUtil.setCode(0);
            mapUtil.setMsg("查询成功");
            mapUtil.setData(recharges);
        }
        else
        {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 充值记录  通过userId查询   用于前台
     */
    @GetMapping("/selectByUserId")
    public Map<String,Object> selectByUserId (@RequestParam("userId") Integer userId)
    {   MapUtil mapUtil = new MapUtil();
        List<Recharge> recharges = rechargeService.selectByUserId(userId);
        System.out.println("list"+recharges);
        if (recharges.size()>0)
        {
            mapUtil.setCode(0);
            mapUtil.setMsg("查询成功");
            mapUtil.setData(recharges);
        }
        else
        {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 充值模糊查询和分页查询接口
     */
    @PostMapping ("/selectLimitLike")
    @ApiOperation(value = "分页查和模糊查询的接口", notes = "提现审核的分页和条件显示方法", response = Map.class, httpMethod = "GET")
    public Map<String, Object> selectLimitLike(@RequestBody @ApiParam(name = "map", readOnly = true, type = "java.util.Map") Map<String, Object> map) {

        List<Recharge> selectList = rechargeService.selectLimitLike(map);
        Integer count = rechargeService.count(map);

        MapUtil mapUtil = new MapUtil();
        if (selectList.size() > 0) {
            mapUtil.put("count", count);
            mapUtil.setData(selectList);
            mapUtil.setCode(0);
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 充值申请接口
     * @param recharge 参数对象   需要参数  userId 用户编号 recpassword 交易密码 rechargeAmount 充值金额
     */
    @PostMapping
    @ApiOperation(value = "充值申请接口", notes = "充值记录以及处理充值订单的方法", response = Map.class, httpMethod = "POST")
    public Map<String, Object> recharge(@RequestBody Recharge recharge) {
        MapUtil mapUtil = new MapUtil();
        Integer verify = rechargeService.verify(recharge);
        if (verify == 0) {
            Integer i = rechargeService.doRecharge(recharge);
            if ( i> 0) {
                mapUtil.setCode(0);
                mapUtil.setMsg("充值成功!");
                return mapUtil.getMap();
            } else {
                throw new BusinessException(ErrorCode.FAILED);
            }
        } else {
            mapUtil.setCode(1);
            mapUtil.setMsg("密码错误");
            return mapUtil.getMap();
        }
    }

}
